package com.sikaryofficial.backend.service.user;

import com.sikaryofficial.backend.constant.ReportBusinessTypeEnum;
import com.sikaryofficial.backend.domain.entity.ArticleEvaluate;
import com.sikaryofficial.backend.domain.entity.ArticleEvaluateReply;
import com.sikaryofficial.backend.domain.entity.BrandTasteEvaluate;
import com.sikaryofficial.backend.domain.entity.BrandTasteEvaluateReply;
import com.sikaryofficial.backend.domain.entity.Store;
import com.sikaryofficial.backend.mapper.ArticleEvaluateMapper;
import com.sikaryofficial.backend.mapper.ArticleEvaluateReplyMapper;
import com.sikaryofficial.backend.mapper.BrandTasteEvaluateMapper;
import com.sikaryofficial.backend.mapper.BrandTasteEvaluateReplyMapper;
import com.sikaryofficial.backend.mapper.StoreMapper;
import com.sikaryofficial.backend.resolver.PropertiesTools;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.enums.ArticleSourceEnum;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import com.sikaryofficial.system.api.RemoteArticleService;
import com.sikaryofficial.system.api.model.infomation.ArticleDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

import static com.sikaryofficial.backend.constant.i18n.ExceptionConstant.*;

/**
 * @author : qinjinyuan
 * @desc : 用户举报公共服务
 * @date : 2023/11/06 11:02
 */
@Service
@Slf4j
public class UserReportPubService {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private BrandTasteEvaluateMapper evaluateMapper;
    @Autowired
    private BrandTasteEvaluateReplyMapper replyMapper;
    @Autowired
    private ArticleEvaluateMapper articleEvaluateMapper;
    @Autowired
    private ArticleEvaluateReplyMapper articleEvaluateReplyMapper;
    @Autowired
    private RemoteArticleService articleService;
    @Resource
    private PropertiesTools propertiesTools;

    public void checkRelationObject(ReportBusinessTypeEnum businessTypeEnum, Long objectId) {
        switch (businessTypeEnum) {
            case STORE:
                storeVerify(objectId);
                break;
            case EVALUATE:
                evaluateVerify(objectId);
                break;
            case EVALUATE_REPLY:
                evaluateReplyVerify(objectId);
                break;
            case ARTICLE:
                articleVerify(objectId);
                break;
            case ARTICLE_EVALUATE:
                articleEvaluateVerify(objectId);
                break;
            case ARTICLE_EVALUATE_REPLY:
                articleEvaluateReplyVerify(objectId);
                break;
            default:
                break;
        }
    }

    private void storeVerify(Long objectId) {
        Store store = storeMapper.selectById(objectId);
        if (Objects.nonNull(store) && Objects.nonNull(store.getCreatedBy()) && store.getCreatedBy().equals(SecurityUtils.getUserId())) {
            throw new ServiceException(propertiesTools.getProperties(CANNOT_OPERATE_YOUR_OWN));
        }
    }

    private void evaluateVerify(Long objectId) {
        BrandTasteEvaluate evaluate = evaluateMapper.selectById(objectId);
        if (Objects.nonNull(evaluate) && evaluate.getCreatedBy().equals(SecurityUtils.getUserId())) {
            throw new ServiceException(propertiesTools.getProperties(CANNOT_OPERATE_YOUR_OWN));
        }
    }

    private void evaluateReplyVerify(Long objectId) {
        BrandTasteEvaluateReply evaluateReply = replyMapper.selectById(objectId);
        if (Objects.nonNull(evaluateReply) && evaluateReply.getCreatedBy().equals(SecurityUtils.getUserId())) {
            throw new ServiceException(propertiesTools.getProperties(CANNOT_OPERATE_YOUR_OWN));
        }
    }

    private void articleVerify(Long objectId) {
        R<ArticleDTO> result = articleService.getInfo(objectId);
        if (Objects.isNull(result) || Objects.isNull(result.getData())) {
            throw new ServiceException(propertiesTools.getProperties(OPERATE_OBJECT_NOT_EXIST));
        }
        ArticleDTO article = result.getData();
        if (Objects.nonNull(article) && Objects.nonNull(article.getCreatedBy()) && article.getCreatedBy().equals(SecurityUtils.getUserId())) {
            throw new ServiceException(propertiesTools.getProperties(CANNOT_OPERATE_YOUR_OWN));
        }
        if (Objects.nonNull(article) && Objects.nonNull(article.getSource()) && ArticleSourceEnum.SYSTEM.getCode().equals(article.getSource())) {
            throw new ServiceException(propertiesTools.getProperties(CANNOT_OPERATE_YOUR_OWN));
        }
    }

    private void articleEvaluateVerify(Long objectId) {
        ArticleEvaluate articleEvaluate = articleEvaluateMapper.selectById(objectId);
        if (Objects.nonNull(articleEvaluate) && articleEvaluate.getCreatedBy().equals(SecurityUtils.getUserId())) {
            throw new ServiceException(propertiesTools.getProperties(CANNOT_OPERATE_YOUR_OWN));
        }
    }

    private void articleEvaluateReplyVerify(Long objectId) {
        ArticleEvaluateReply articleEvaluateReply = articleEvaluateReplyMapper.selectById(objectId);
        if (Objects.nonNull(articleEvaluateReply) && articleEvaluateReply.getCreatedBy().equals(SecurityUtils.getUserId())) {
            throw new ServiceException(propertiesTools.getProperties(CANNOT_OPERATE_YOUR_OWN));
        }
    }
}
